﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using mshtml;
using Timer = System.Windows.Forms.Timer;

namespace ProductManage
{
    public partial class FormGetAmazonProducts : Form
    {
        private int iListUrl = 0;
        private List<string> listUrl = new List<string>();
        private List<string> DownloadlistUrl = new List<string>();
        private List<string> AlllistUrl = new List<string>();
        private string strPath = @"C:\Alex\Amazon";
        public FormGetAmazonProducts()
        {
            InitializeComponent();
        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            ReloadListUrl();
            this.webBrowserCategory.Url = new Uri(this.textBox1.Text);
        }

        #region getProductURL

        private void DealNextPage()
        {
            var links = this.webBrowserCategory.Document.GetElementsByTagName("a");
            foreach (HtmlElement link in links)
            {
                if (link.InnerHtml != null)
                    if (link.InnerHtml.Contains("Next"))
                    {
                        link.InvokeMember("click");
                        return;
                    }
            }
        }

        private void webBrowserCategory_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowserCategory.Document != null)
            {
                try
                {
                    var document = webBrowserCategory.Document;
                    var urls = GetProductUrls(document);


                    foreach (var url in urls)
                    {
                        if (!AlllistUrl.Contains(url))
                        {
                            AlllistUrl.Add(url);
                            StreamWriter writer = new StreamWriter(strPath + @"\Amazonproduct.txt", true);
                            writer.WriteLine(url);
                            writer.Close();
                        }
                    }

                    if (urls.Count == 0)
                    {
                        this.Text = "Done";
                        return;
                    }
                }
                catch
                {
                }

            }
        }

        private List<string> GetProductUrls(HtmlDocument strHtml)
        {
            var infos = strHtml.GetElementById("atfResults");
            List<string> list = new List<string>();
            try
            {
                if (infos == null)
                    return list;
                var index = 0;
                while (infos.InnerHtml.IndexOf("name=\"", index + 1) > 0)
                {
                    index = infos.InnerHtml.IndexOf("name=\"", index + 1);
                    var endIndex = infos.InnerHtml.IndexOf("\"", index + 6);
                    list.Add(infos.InnerHtml.Substring(index + 6, endIndex - index - 6));
                }
            }
            catch (Exception)
            {
            }
            return list;
        }

        #endregion

        private Timer timer;
        private Timer timer1;
        private void button1_Click(object sender, EventArgs e)
        {
            if (timer == null)
            {
                timer = new Timer();
                timer.Interval = 200;
                timer.Tick += timer_Tick;
            }
            if (timer1 == null)
            {
                timer1 = new Timer();
                timer1.Interval = 3000;
                timer1.Tick += timer1_Tick;
            }
            if (this.button1.Text != "working")
            {
                this.button1.Text = "working";
                timer.Start();
                timer1.Start();
            }
            else
            {
                this.button1.Text = "Stopped";
                timer.Stop();
                timer1.Stop();
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            webBrowserCategory_DocumentCompleted(null, null);
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            DealNextPage();
        }
        private List<string> ReloadLoadedUrl()
        {
            var listDownLoad = new List<string>();
            if (File.Exists(strPath + @"\AmazonProductInfos.csv"))
            {
                StreamReader reader1 = new StreamReader(strPath + @"\AmazonProductInfos.csv");
                while (!reader1.EndOfStream)
                {
                    try
                    {
                        var strLine = reader1.ReadLine();
                        if (strLine != null)
                        {
                            var url = strLine.Split(',')[0];
                            listDownLoad.Add(url);
                        }

                    }
                    catch (Exception)
                    {
                    }
                }
                reader1.Close();
            }
            return listDownLoad;
        }
        private void ReloadListUrl()
        {
            iListUrl = -1;
            var listDownLoad = ReloadLoadedUrl();
            listUrl.Clear();
            if (!File.Exists(strPath + @"\Amazonproduct.txt"))
            {
                var writer = File.CreateText(strPath + @"\Amazonproduct.txt");
                writer.Close();
            }
            StreamReader reader = new StreamReader(strPath + @"\Amazonproduct.txt");
            while (!reader.EndOfStream)
            {
                var strLine = reader.ReadLine();
                AlllistUrl.Add(strLine);
                if (!listDownLoad.Contains(strLine))
                    listUrl.Add(strLine);
            }
            reader.Close();
        }




        private Timer timer2;
        private Dictionary<string, string> dicDetail = new Dictionary<string, string>();
        private List<string> listPics = new List<string>();
        private void GetProductDate()
        {
            timer2.Stop();
            if (webBrowserProduct.Document != null)
            {
                try
                {
                    var document = webBrowserProduct.Document;
                    GetProductDetail(document);
                    GetPicUrl(document);
                    if (!dicDetail.ContainsKey("Item model number"))
                    {
                        if (dicDetail.Count > 1)
                        {
                            if (webBrowserProduct.Url.ToString() == listUrl[iListUrl])
                            {
                                dicDetail = new Dictionary<string, string>();
                                listPics = new List<string>();
                                if (iListUrl++ < listUrl.Count - 1)
                                {
                                    var url = listUrl[iListUrl];
                                    this.webBrowserProduct.Url = new Uri(url);
                                }
                                else
                                {
                                    this.Text = "done";
                                    button2_Click(null, null);
                                    return;
                                }
                            }
                        }
                        timer2.Interval = 1000;
                        timer2.Start();
                        return;
                    }
                    try
                    {
                        if (!DownloadlistUrl.Contains(webBrowserProduct.Url.ToString()))
                        {
                            #region Save

                            var strUrl = webBrowserProduct.Url.ToString();
                            var code = dicDetail["Item model number"];
                            var weight = "";
                            if (dicDetail.ContainsKey("Weight"))
                                weight = dicDetail["Weight"];
                            var size = "";
                            if (dicDetail.ContainsKey("Product Dimensions"))
                                size = dicDetail["Product Dimensions"].Replace(',', ' ');
                            var color = "";
                            if (dicDetail.ContainsKey("Hole size"))
                                color = dicDetail["Hole size"];
                            var title = "";
                            if (dicDetail.ContainsKey("title"))
                                title = dicDetail["title"].Replace(',', ' ');
                            var productDescription = "";
                            if (dicDetail.ContainsKey("productDescription"))
                                productDescription = dicDetail["productDescription"].Replace(',', ' ');


                            string strPics = "";
                            foreach (var pic in listPics)
                            {
                                string localPath = strPath + @"\Image\" + Path.GetFileName(pic);
                                if (!File.Exists(localPath))
                                {
                                    WebClient mywebclient = new WebClient();
                                    mywebclient.DownloadFile(pic, localPath);
                                }
                                strPics += ',' + Path.GetFileName(pic);
                            }

                            StreamWriter writer = new StreamWriter(strPath + @"\AmazonProductInfos.csv", true);
                            writer.Write(strUrl + ',' + code + ',' + weight + ',' + size + ',' + color + ',' +
                                         ',' + title + ',' + productDescription + ',' +
                                         strPics + "\r\n");
                            writer.Close();

                            #endregion

                            DownloadlistUrl.Add(listUrl[iListUrl]);
                        }
                        else
                        {

                            if (webBrowserProduct.Url.ToString() == listUrl[iListUrl])
                            {
                                dicDetail = new Dictionary<string, string>();
                                listPics = new List<string>();
                                if (iListUrl++ < listUrl.Count - 1)
                                {
                                    var url = listUrl[iListUrl];
                                    this.webBrowserProduct.Url = new Uri(url);
                                }
                                else
                                {
                                    this.Text = "done";
                                    button2_Click(null, null);
                                    return;
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                    }
                }
                catch (Exception exception)
                {
                }
            }

            timer2.Interval = 1000;
            timer2.Start();
        }
        //int indexproductImageselect = 0;
        private void DealNextProduct()
        {
            //var links = this.webBrowserProduct.Document.GetElementsByTagName("li");
            //int i = 0;
            //foreach (HtmlElement link in links)
            //{
            //    if (link.OuterHtml != null)
            //        if (link.OuterHtml.Contains("a-spacing-small item"))
            //        {
            //            var inputs = link.GetElementsByTagName("input");
            //            foreach (HtmlElement input in inputs)
            //            {
            //                if (i == indexproductImageselect)
            //                {
            //                    input.InvokeMember("Click");
            //                    indexproductImageselect = indexproductImageselect + 1;
            //                    timer2.Interval = 1000;
            //                    timer2.Start();
            //                    return;
            //                }
            //                else
            //                {
            //                    i++;
            //                }
            //            }
            //        }
            //}
            //indexproductImageselect = 0;

           

        }
        private bool GetPicUrl(HtmlDocument strHtml)
        {
            bool bRtn = false;
            var infos = strHtml.GetElementById("main-image-container");
            try
            {
                if (infos == null)
                    return bRtn;
                var index = 0;
                while (infos.InnerHtml.IndexOf("http://ecx.images-amazon.com/images/", index + 1) > 0)
                {
                    index = infos.InnerHtml.IndexOf("http://ecx.images-amazon.com/images/", index + 1);
                    var endIndex = infos.InnerHtml.IndexOf("\"", index);
                    var url = infos.InnerHtml.Substring(index, endIndex - index);
                    if (!url.Contains("gif"))
                    {
                        if (!listPics.Contains(url))
                        {
                            bRtn = true;
                            listPics.Add(url);
                        }
                    }
                }
            }
            catch
            {
            }
            return bRtn;
        }
        private string GetProductDetail(HtmlDocument strHtml)
        {
            string snumber = "";
            var infos = strHtml.GetElementById("feature-bullets");
            try
            {
                if (infos == null)
                    return snumber;
                var text = infos.InnerText;
                StringReader reader = new StringReader(text);
                var strLine = reader.ReadLine();
                while (!string.IsNullOrEmpty(strLine))
                {
                    if (strLine.Contains(":"))
                    {
                        var t = strLine.Split(':');
                        if (!dicDetail.ContainsKey(t[0]))
                        {
                            dicDetail.Add(t[0].Trim(), t[1].Trim());
                        }
                        else
                        {
                            dicDetail[t[0].Trim()] =  t[1].Trim();
                        }
                    }
                    strLine = reader.ReadLine();
                }
            }
            catch (Exception)
            {
            }

            infos = strHtml.GetElementById("detailBullets_feature_div");
            try
            {
                if (infos == null)
                    return snumber;
                var text = infos.InnerText;
                StringReader reader = new StringReader(text);
                var strLine = reader.ReadLine();
                while (!string.IsNullOrEmpty(strLine))
                {
                    if (strLine.Contains(":"))
                    {
                        var t = strLine.Split(':');
                        if (t[0].Trim() == "Item model number") snumber = t[1].Trim();
                        if (!dicDetail.ContainsKey(t[0].Trim()))
                        {
                            dicDetail.Add(t[0].Trim(), t[1].Trim());
                        }
                    }
                    strLine = reader.ReadLine();
                }
            }
            catch (Exception)
            {
            }

            infos = strHtml.GetElementById("title"); 
            if (infos == null)
                return snumber;
            if (!dicDetail.ContainsKey("title"))
            {
                dicDetail.Add("title", infos.OuterHtml);
            }

            infos = strHtml.GetElementById("productDescription"); 
            if (infos == null)
                return snumber;
            if (!dicDetail.ContainsKey("productDescription"))
            {
                dicDetail.Add("productDescription", infos.InnerText);
            }

            
            return snumber;
        }
        private string GetProductSKU(HtmlDocument strHtml)
        {
            string snumber = "";
            var infos = strHtml.GetElementById("detailBullets_feature_div");
            try
            {
                if (infos == null)
                    return snumber;
                var text = infos.InnerText;
                StringReader reader = new StringReader(text);
                var strLine = reader.ReadLine();
                while (!string.IsNullOrEmpty(strLine))
                {
                    if (strLine.Contains(":"))
                    {
                        var t = strLine.Split(':');
                        if (t[0].Trim() == "Item model number") snumber = t[1].Trim();
                        if (!dicDetail.ContainsKey(t[0].Trim()))
                        {
                            dicDetail.Add(t[0].Trim(), t[1].Trim());
                        }
                    }
                    strLine = reader.ReadLine();
                }
            }
            catch (Exception)
            {
            }
            return snumber;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (timer2 == null)
            {
                timer2 = new Timer();
                timer2.Interval = 1000;
                timer2.Tick += timer2_Tick;
            }
            if (this.button2.Text != "working")
            {
                this.button2.Text = "working";
                if (iListUrl++ < listUrl.Count - 1)
                {
                    var url = listUrl[iListUrl];
                    this.webBrowserProduct.Url = new Uri(url);
                }
                timer2.Start();
            }
            else
            {
                this.button2.Text = "Stopped";
                timer2.Stop();
            }
        }
        void timer2_Tick(object sender, EventArgs e)
        {
            GetProductDate();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }



        
    }
}
